<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>类的本质 | OC中的私有方法</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="./14.html" />
    
    
    <link rel="prev" href="./13.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="gitbook/style.css">


        
    <div class="book"  data-level="11" data-basepath="." data-revision="1442488483454">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                         OC中的私有方法 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="property/README.html">
            
                
                    <a href="./property/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         @property基本概念 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="synthesize/README.html">
            
                
                    <a href="./synthesize/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         @synthesize基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="5.html">
            
                
                    <a href="./5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         @property增强
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="6.html">
            
                
                    <a href="./6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         @property修饰符
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="id/README.html">
            
                
                    <a href="./id/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         id类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="new/README.html">
            
                
                    <a href="./new/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         new方法实现原理
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="9.html">
            
                
                    <a href="./9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="10.html">
            
                
                    <a href="./10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="12.html">
            
                
                    <a href="./12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         继承中的自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="13.html">
            
                
                    <a href="./13.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         自定义类工厂方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="11" data-path="1.html">
            
                
                    <a href="./1.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         类的本质
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="14.html">
            
                
                    <a href="./14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         类的启动过程
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="sel/README.html">
            
                
                    <a href="./sel/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         SEL类型
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >OC中的私有方法</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_319">
                    
                        <h1 id="">类的本质</h1>
<h2 id="">本小节知识:</h2>
<ol>
<li>【了解】类的本质</li>
<li>【掌握】如何获取类对象</li>
<li>【理解】类对象的用法</li>
<li>【理解】类对象的存储</li>
<li>【了解】OC实例对象类对象元数据之间关系</li>
</ol>
<hr>
<h2 id="1">1.类的本质</h2>
<ul>
<li>类的本质其实也是一个对象(类对象)</li>
<li>程序中第一次使用该类的时候被创建，在整个程序中只有一份。</li>
<li>此后每次使用都是这个类对象，它在程序运行时一直存在。</li>
<li>类对象是一种数据结构,存储类的基本信息:类大小,类名称,类的版本，继承层次，以及消息与函数的映射表等</li>
<li>类对象代表类,Class类型,对象方法属于类对象</li>
<li>如果消息的接收者是类名,则类名代表类对象</li>
<li>所有类的实例都由类对象生成,类对象会把实例的isa的值修改成自己的地址,每个实例的isa都指向该实例的类对象</li>
</ul>
<hr>
<h2 id="2">2.如何获取类对象</h2>
<ul>
<li>通过实例对象</li>
</ul>
<pre><code class="lang-objc">格式：[实例对象   class ];
如：   [dog class];
</code></pre>
<ul>
<li>通过类名获取(类名其实就是类对象)</li>
</ul>
<pre><code class="lang-objc">格式：[类名 class];
如：[Dog class]
</code></pre>
<hr>
<h2 id="3">3.类对象的用法</h2>
<ul>
<li>用来调用类方法</li>
</ul>
<pre><code class="lang-objc">[Dog test];

Class c = [Dog class];
[c test];
</code></pre>
<ul>
<li>用来创建实例对象</li>
</ul>
<pre><code class="lang-objc">Dog *g = [Dog new];

Class c = [Dog class];
Dog *g1 = [c new];
</code></pre>
<hr>
<h2 id="4">4.类对象的存储</h2>
<p><img src="images/a11/ldxcc.png" alt=""></p>
<hr>
<h2 id="5oc--">5.OC实例对象 类对象 元对象之间关系</h2>
<ul>
<li><p>Objective-C是一门面向对象的编程语言。</p>
<ul>
<li>每一个对象 都是一个类的实例。</li>
<li>每一个对象 都有一个名为isa的指针,指向该对象的类。</li>
<li>每一个类􏰁述了一系列它的实例的特点,包括成员变量的列表,成员函数的列表等。</li>
<li>每一个对象都可以接受消息,而对象能够接收的消息列表是保存在它所对应的类中。</li>
</ul>
</li>
<li><p>在Xcode中按Shift + Command + O打开文件搜索框，然后输入NSObject.h和objc.h,可以打开 NSObject的定义头文件,通过头文件我们可以看到,NSObject就是一个包含isa指针的结构体,如下图所示:</p>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-built_in">NSObject</span><span class="hljs-variable">.h</span>
<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">NSObject</span> &lt;<span class="hljs-title">NSObject</span>&gt; </span>{
    Class isa  OBJC_ISA_AVAILABILITY;
}
</code></pre>
<pre><code class="lang-objc">objc<span class="hljs-variable">.h</span>
<span class="hljs-comment">/// An opaque type that represents an Objective-C class.</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> objc_class *Class;

<span class="hljs-comment">/// Represents an instance of a class.</span>
<span class="hljs-keyword">struct</span> objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};
</code></pre>
<ul>
<li>按照面向对象语言的设计原则,所有事物都应该是对象(严格来说 Objective-C并没有完全做到这一点,因为它有int,double这样的简单变量类型)<ul>
<li>在Objective-C语言中,每一个类实际上也是一个对象。每一个类也有一个名为isa的指针。每一个类都可以接受消息,例如[NSObject new],就是向NSObject这个类发送名为new的消息。</li>
<li>在Xcode中按Shift + Command + O,然后输入runtime.h,可以打开Class的定义头文件,通过头文件我们可以看到,Class也是一个包含isa指针的结构体,如下图所示。(图中除了isa外还有其它成员变量,但那是为了兼容非2.0版的Objective-C的遗留逻辑,大家可以忽略它。)</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">runtime<span class="hljs-variable">.h</span>
<span class="hljs-keyword">struct</span> objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

<span class="hljs-preprocessor">#if !__OBJC2__</span>
    Class super_class                                        OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *name                                         OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">long</span> version                                             OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">long</span> info                                                OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">long</span> instance_size                                       OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">struct</span> objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">struct</span> objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">struct</span> objc_cache *cache                                 OBJC2_UNAVAILABLE;
    <span class="hljs-keyword">struct</span> objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
<span class="hljs-preprocessor">#endif</span>

} OBJC2_UNAVAILABLE;
</code></pre>
<ul>
<li><p>因为类也是一个对象,那它也必须是另一个类的实例,这个类就是元类 (metaclass)。</p>
<ul>
<li>元类保存了<code>类方法</code>的列表。当一个<code>类方法</code>被调用时,元类会首先查找它本身是否有该类方法的实现,如果没有则该元类会向它的父类查找该方法,直到一直找到继承链的头。</li>
<li>元类(metaclass)也是一个对象,那么元类的isa指针又指向哪里呢?为了设计上的完整,所有的元类的isa指针都会指向一个根元类(root metaclass)。</li>
<li>根元类(root metaclass)本身的isa指针指向自己,这样就行成了一个闭环。上面说􏰀到,一个对象能够接收的消息列表是保存在它所对应的类中的。在实际编程中,我们几乎不会遇到向元类发消息的情况,那它的isa 指针在实际上很少用到。不过这么设计保证了面向对象的干净,即所有事物都是对象,都有isa指针。</li>
<li>由于<code>类方法</code>的定义是保存在元类(metaclass)中,而方法调用的规则是,如果该类没有一个方法的实现,则向它的父类继续查找。所以为了保证父类的类方法可以在子类中可以被调用,所以子类的元类会继承父类的元类,换而言之,类对象和元类对象有着同样的继承关系。</li>
</ul>
</li>
<li><p>下面这张图或许能够 让大家对isa和继承的关系清楚一些</p>
</li>
</ul>
<p><img src="images/a11/sldxldxydx.png" alt=""></p>
<ul>
<li>上图中,最让人困惑的莫过于Root Class了。在实现中,Root Class是指
NSObject,我们可以从图中看出:</li>
<li>NSObject类对象包括它的对象实例方法。</li>
<li>NSObject的元对象包括它的类方法,例如new方法。</li>
<li>NSObject的元对象继承自NSObject类。</li>
<li>一个NSObject的类中的方法同时也会被NSObject的子类在查找方法时找到。</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./13.html" class="navigation navigation-prev " aria-label="Previous page: 自定义类工厂方法"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./14.html" class="navigation navigation-next " aria-label="Next page: 类的启动过程"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
